import { createApp } from 'vue'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
// import zhCn from 'element-plus/es/locale/lang/zh-cn' // 改变成中文样式
import ja from 'element-plus/es/locale/lang/ja'
import './assets/css/global.css'
import './assets/iconfont/iconfont.css'
import './assets/css/reset.css'
import '@fullcalendar/daygrid/main.css';
import '@fullcalendar/timegrid/main.css';
import axios from 'axios'
import VueAxios from 'vue-axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import App from './App.vue'
import router from './router'
import store from './store'
import getMsg from './message'
import 'lib-flexible-computer'
import { ElMessage } from 'element-plus'
// import 'default-passive-events'

axios.defaults.baseURL = 'http://122.192.3.226:4025/ohsOaApi'
// axios.defaults.baseURL = 'http://172.24.7.178:8000/ohsOaApi'
// axios.defaults.baseURL = 'http://172.24.7.180:8001/ohsOaApi'
// axios.defaults.baseURL = 'http://172.24.7.184:8001/ohsOaApi'
// axios.defaults.baseURL = 'http://localhost:8001/ohsOaApi'


// axios请求拦截器
axios.interceptors.request.use(config => {
  if (window.localStorage.getItem('token') !== null) {
    config.headers.common['token'] = window.localStorage.getItem('token')
  }
  NProgress.start()
  return config
})

// 响应拦截
axios.interceptors.response.use(response => {

  if (response.data.token !== undefined && response.data.token !== null) {
    window.localStorage.setItem('token', response.data.token)
  }

  console.log(response);
  NProgress.done()
  return response
}, error => {
  console.log(error)
  console.log(error.response)

  const response = error.response;
  window.localStorage.removeItem("token")
  router.push("/login")
  if (response.status !== undefined && response.status == 401) {
    ElMessage({ message: response.data.msg, type: 'warning', })
  } else {
    ElMessage({ message: getMsg("E0001"), type: 'error', })
  }
  return Promise.reject()
})
const app = createApp(App)
app.config.globalProperties.$getMsg = getMsg

app.use(router)
app.use(ElementPlus, {
  locale: ja,
  size: "medium"
})
app.use(VueAxios, axios)
app.use(store)
app.directive('preventReClick', {
  mounted(el) {
    el.addEventListener('click', () => {
      if (!el.disabled) {
        el.disabled = true;
        setTimeout(() => {
          el.disabled = false;
        }, 800
        )
      }
    })
  }
})

app.mount('#app')
